package com.heima.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.user.UserConstants.AdminConstans;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.pojos.WmUser;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.feign.ApAuthorFeign.ArticleFeign;
import com.heima.user.feign.WmUserFeign.WemediaFeign;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService{
    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 审核列表
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        //1.校验参数
        if (dto==null){
            return(PageResponseResult) PageResponseResult.setAppHttpCodeEnum(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam(); //2.获取页码信息

        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<ApUserRealname>();
        if (dto.getStatus()!=null){
          lambdaQueryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        IPage pageParm = new Page(dto.getPage(),dto.getSize());
        IPage page = page(pageParm,lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(),(int)page.getTotal());
        pageResponseResult.setCode(200);
       /* List records = page.getRecords();
        System.out.println(list());*/
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;

    }

    /**
     * 修改状态，根据前端传值
     * @param dto
     * @param status
     * @return
     */
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Short status) {

        //1.校验参数
        if (dto==null||dto.getId()==null){
            return ResponseResult.setAppHttpCodeEnum(AppHttpCodeEnum.PARAM_REQUIRE);//缺少参数
        }
        if (statusCheck(status)){
            return ResponseResult.setAppHttpCodeEnum(AppHttpCodeEnum.PARAM_REQUIRE);//缺少参数
        }
        //2.修改状态
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(status);
        if (dto.getMsg()!=null){
            apUserRealname.setReason(dto.getMsg());
        }
        System.out.println(apUserRealname);
        updateById(apUserRealname);
        //3.认证通过的要添加自媒体以及作者账号
        if (status.equals(AdminConstans.PASS_AUTH)){
            ResponseResult createResult = createWmUserAndAuthor(dto);
            if (createResult != null) {
                return createResult;
            }
            //TODO 发送通知消息
        }
        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /**
     * 创建自媒体账号， 以及作者账号
     * @param dto
     * @return
     */
    private ResponseResult createWmUserAndAuthor(AuthDto dto){
        ApUserRealname apUserRealname = getById(dto.getId());
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);//参数有误
        }
        //检测自媒体用户是否存在
        WmUser wmUser = wemediaFeign.findByName(apUser.getName());

        if (wmUser==null||wmUser.getId()==null){
            wmUser = new WmUser();
            //设置ApUserId
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
            wmUser.setApUserId(apUser.getId());
            wemediaFeign.save(wmUser);
        }
        //创建作者账号
        createAuthor(wmUser);
        apUser.setFlag(1);
        apUserMapper.updateById(apUser);
        return null;
    }
    /**
     * 创建自媒体账号
     * @param wmUser
     * @return
     */
    private void createAuthor(WmUser wmUser) {
        Integer apUserId = wmUser.getApUserId();
        ApAuthor apAuthor =  articleFeign.findByUserId(Integer.valueOf(apUserId));
        if (apAuthor == null) {
            apAuthor = new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(AdminConstans.AUTHOR_TYPE);
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(apUserId);
            articleFeign.save(apAuthor);
        }
    }
    /**
     * 状态监测
     * @param status
     * @return
     */
    private boolean statusCheck(Short status) {
        if (status == null
                || ( !status.equals(AdminConstans.FAIL_AUTH)
                && !status.equals(AdminConstans.PASS_AUTH))) {
            return true;
        }
        return false;
    }
}
